<template>
<!-- TODO：应该是没用了，后续确认清楚删掉~ -->
  <div class="search-panel-product">
    <base-panel :show.sync="visible" :width="panelWidth">
      <h3 slot="title">
        <span>高级搜索</span>
        <el-dropdown
          class="pull-right"
          trigger="click"
          @command="setAdvanceSearchColumn"
        >
          <i
            class="iconfont icon-xitongguanli customer-panel-btn"
            style="float: none"
          ></i>

          <el-dropdown-menu slot="dropdown" class="customer-advance-setting">
            <el-dropdown-item command="1">一栏</el-dropdown-item>
            <el-dropdown-item command="2">两栏</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </h3>
      <div
        class="task-search-panel-title task-pointer task-flex task-ai"
        @click="show = !show"
      >
        <span class="task-font16">常用查询条件</span>
        <span
          slot="reference"
          class="task-font14 task-c2 task-ml12 task-mr4"
          @click.stop="$refs.searchSetModal.open()"
          >设置</span
        >
        <span class="task-span1">
          <el-tooltip
            content="常用查询条件可以通过“设置”功能，进行添加和修改"
            placement="top"
          >
            <i class="iconfont icon-question task-icon"></i>
          </el-tooltip>
        </span>
        <i class="iconfont icon-triangle-down task-f12 task-c9" v-if="!show"></i>
        <i class="iconfont icon-up task-icon" v-else></i>
      </div>
      <el-form label-position="top" class="advanced-search-form" onsubmit="return false;">
        <search-form
          v-show="show"
          :fields="diyFields"
          ref="searchForm"
          :form-backup="formBackup"
          :column-num="columnNum"
        ></search-form>
        <slot name="footer"></slot>
      </el-form>

      <!-- 设置弹框 -->
      <search-set-modal
        ref="searchSetModal"
        :fields="fields"
        @changeDiyFields="changeDiyFields"
      />
    </base-panel>

    <!-- 遮罩层 -->
    <div v-if="visible" class="cover"></div>
  </div>
</template>

<script>
import { FormFieldMap, SettingComponents } from '@src/component/form/components';
import {
  PRODUCT_SEARCH_MODAL_COM_DATA,
  PRODICT_LIST_SEARCHPANEL_LOCALSTORAGE,
} from '@src/modules/productV2/storage';
import * as Utils from '@src/component/form/util';
import { storageGet, storageSet } from '@src/util/storage';
import _ from 'lodash';
import { isEmptyStringObject } from '@src/util/function';

import SearchSetModal from './SearchSetModal';
import { isFunction } from '@src/util/type';
import { safeNewDate } from '@src/util/time';
/* service */
import { convertFormQualityInfoFieldValueToQualityInfoRemoteData } from '@service/QualityInfoService.ts';
import { baseDistValueToObject, searchAddressObjectParams} from '@src/util/addressUtil';
import { formatAddress, formatDate } from 'pub-bbx-utils';

export default {
  name: 'search-panel',
  props: {
    config: {
      type: Object,
      default: () => ({}),
    },
    initData: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      visible: false,
      formBackup: {},
      columnNum: 1,
      show: true,
      diyFields: [],
    };
  },
  computed: {
    fields() {
      let fields = this.config.fields;

      let obj_ = storageGet(PRODUCT_SEARCH_MODAL_COM_DATA);
      if (obj_) {
        obj_ = JSON.parse(obj_);
        if (obj_.allCheckList.length) {
          this.diyFields = fields.filter(
            (item) => obj_.allCheckList.indexOf(item.fieldName) > -1
          );
        }
      }

      return fields;
    },
    panelWidth() {
      return `${420 * this.columnNum}px`;
    },
  },
  mounted() {
    const { column_number } = this.getLocalStorageData();
    if (column_number) this.columnNum = Number(column_number);
  },
  methods: {
    saveDataToStorage(key, value) {
      const data = this.getLocalStorageData();
      data[key] = value;
      localStorage.setItem(PRODICT_LIST_SEARCHPANEL_LOCALSTORAGE, JSON.stringify(data));
    },
    getLocalStorageData() {
      const dataStr = localStorage.getItem(PRODICT_LIST_SEARCHPANEL_LOCALSTORAGE) || '{}';
      return JSON.parse(dataStr);
    },
    resetParams() {
      this.formBackup = {};
      this.$refs.searchForm && this.$nextTick(this.$refs.searchForm.initFormVal);
    },

    buildParams(formData) {
      let form;
      try {
        form = formData || this.$refs.searchForm.returnData();
      } catch (error) {
        console.warn(error, 'error try catch');
      }
      console.log(form, '****999***');
      this.formBackup = Object.assign({}, form);

      const isSystemFields = this.fields.filter((f) => f.isSystem);
      const notSystemFields = this.fields.filter((f) => !f.isSystem);
      let params = {
        conditions: [],
      };

      let tv = null;
      let fn = '';

      for (let i = 0; i < isSystemFields.length; i++) {
        tv = isSystemFields[i];
        fn = tv.fieldName;

        // hasRemind
        if (fn === 'hasRemind' && form[fn] !== '') {
          params.hasRemind = form[fn] == 2 ? 0 : form[fn];
          continue;
        }

        if (fn === 'qrcodeState' && form[fn] !== '') {
          params.qrcodeState = form[fn] == 2 ? 0 : form[fn];
          continue;
        }

        if (fn === 'productCompleteAddress') {
          let isEmpty = isEmptyStringObject(form[fn]);
          let value = '';
          if (!isEmpty) {
            value = formatAddress(form[fn],'');
          }
          params['productCompleteAddress'] = value;
        }
        if (fn == 'area') {
          const address = baseDistValueToObject(form[fn])
          params.productAddress = {
            ...(params.productAddress || {}),
            ...(searchAddressObjectParams(address))
          };
          continue;
        }

        if (fn === 'addressDetail') {
          params.productAddress = {
            ...(params.productAddress || {}),
            address: form[fn],
          };
          continue;
        }

        if (!form[fn] || (Array.isArray(form[fn]) && !form[fn].length)) {
          continue;
        }

        if (tv.formType === 'date' || tv.formType === 'datetime') {
          params[fn] = form[fn].map((t) => formatDate(t, 'YYYY/MM/DD')).join('-');
          continue;
        }

        if (tv.formType === 'tags') {
          params.searchTagId = form[fn].map(({ id }) => id).join('');
        }

        if (typeof form[fn] === 'string') {
          params[fn === 'customer' ? 'customerId' : fn] = form[fn];
          continue;
        }

        if (typeof form[fn] === 'number') {
          params[fn] = form[fn];
          continue;
        }
      }

      for (let i = 0; i < notSystemFields.length; i++) {
        let key = null;
        tv = notSystemFields[i];
        fn = tv.fieldName;

        if (!form[fn] || (Array.isArray(form[fn]) && !form[fn].length)) {
          continue;
        }

        // 空对象
        if (
          typeof form[fn] === 'object' &&
          !Array.isArray(form[fn]) &&
          !Object.keys(form[fn]).length
        ) {
          continue;
        }

        if (tv.originalFormType === 'date') {
          params.conditions.push({
            property: fn,
            operator: tv.operator,
            betweenValue1: formatDate(form[fn][0], 'YYYY-MM-DD'),
            betweenValue2: formatDate(form[fn][1], 'YYYY-MM-DD'),
          });
          continue;
        }

        if (tv.originalFormType === 'datetime') {
          params.conditions.push({
            property: fn,
            operator: tv.operator,
            betweenValue1: formatDate(form[fn][0], 'YYYY-MM-DD HH:mm:ss'),
            betweenValue2: `${formatDate(form[fn][1], 'YYYY-MM-DD')} 23:59:59`,
          });
          continue;
        }

        if (tv.formType === 'cascader') {
          params.conditions.push({
            property: fn,
            operator: tv.operator,
            inValue: form[fn],
          });
          continue;
        }

        if (tv.formType === 'address') {
          let address = {
            property: fn,
            operator: tv.operator,
          };
          let isEmpty = isEmptyStringObject(form[fn]);

          if (!isEmpty) {
            address.value = formatAddress(form[fn],'');
          }
          params.conditions.push(address);
          continue;
        }

        if (tv.originalFormType === 'related_task') {
          key = 'taskNo';
        }

        params.conditions.push({
          property: fn,
          operator: tv.operator,
          value: form[fn],
          key,
        });
      }
      // 质保信息数据处理
      params = Object.assign(
        params,
        convertFormQualityInfoFieldValueToQualityInfoRemoteData(params)
      );
      // 返回接口数据
      // 创建时间
      const createTimeStart = this._time(params.createTime, 0);
      const createTimeEnd = this._time(params.createTime, 1);
      // 更新时间
      const updateTimeStart = this._time(params.updateTime, 0);
      const updateTimeEnd = this._time(params.updateTime, 1);
      params.createTimeStart = createTimeStart;
      params.createTimeEnd = createTimeEnd;
      params.updateTimeStart = updateTimeStart;
      params.updateTimeEnd = updateTimeEnd;
      delete params.createTime;
      delete params.updateTime;
      return params;
    },
    /**
     * @description 时间字符串切割
     */
    _time(params, num) {
      if (!params) return;
      if (params && !isNaN(num)) {
        let S, E;
        if (num === 1) {
          E = `${params.split('-')[1]} 23:59:59`;
        } else {
          S = `${params.split('-')[0]} 00:00:00`;
        }
        return safeNewDate([S, E][num]);
      }
      return safeNewDate(params);
    },
    setAdvanceSearchColumn(command) {
      this.columnNum = Number(command);
      this.saveDataToStorage('column_number', command);
    },
    open() {
      this.visible = true;
    },
    changeDiyFields(e) {
      if (e.length) (this.diyFields = e), (this.show = true);
    },
  },
  components: {
    [SearchSetModal.name]: SearchSetModal,
    SearchForm: {
      name: 'search-form',
      props: {
        fields: {
          type: Array,
          default: () => [],
        },
        formBackup: {
          type: Object,
          default: () => ({}),
        },
        columnNum: {
          type: Number,
          default: 1,
        },
      },
      data() {
        return {
          form: {},
          tableName: 'product',
          show: true,
        };
      },
      mounted() {
        this.buildForm();
      },
      methods: {
        returnData() {
          return Object.assign({}, this.form);
        },
        buildForm() {
          if (Object.keys(this.form).length === this.fields.length) return;
          this.initFormVal();
        },
        initFormVal() {
          let fields = this.fields;
          let form = {};
          let tv = '';

          fields.forEach((field) => {
            tv = '';
            // 地址的默认值初始化为对象
            if (field.formType == 'customerAddress' || field.formType == 'address')
              tv = {};
            if (field.formType == 'date' || field.formType == 'datetime') tv = [];
            if (field.formType === 'link') {
              tv = {};
            }
            if (field.formType === 'tags') {
              tv = [];
            }
            if (field.formType === 'area') {
              tv = [];
            }
            if (field.formType === 'area') {
              tv = [];
            }
            if (field.formType === 'cascader') {
              tv = [];
            }
            if (field.formType === 'select' && field.displayName === '服务部门') {
              tv = [];
            }
            form[field.fieldName] = this.formBackup[field.fieldName] || tv;

            this.$set(this.form, field.fieldName, this.formBackup[field.fieldName] || tv);
          });

          return form;
        },
        update(event, action) {
          if (action === 'tags') {
            return this.$set(this.form, 'tags', event);
          }

          if (action === 'dist') {
            return this.$set(this.form, 'area', event);
          }

          const field = event.field;
          if (field.returnData) {
            event.newValue = isFunction(field.returnData)
              ? field.returnData(event.newValue)
              : event.newValue[field.returnData] || event.newValue;
          }

          this.$set(this.form, field.fieldName, event.newValue);
        },
        createUserInput(event, isTags) {
          if (isTags) {
            return (this.form.tags = event);
          }
          const f = event.field;
          this.form[f.fieldName] = event.keyword;
        },
        renderInput(h, field) {
          const f = {
            ...Object.freeze(field),
          };

          let comp = FormFieldMap.get(f.formType);

          if (
            !comp &&
            f.formType !== 'tags' &&
            f.formType !== 'area' &&
            f.formType !== 'linkman'
          ) {
            return null;
          }

          if (f.formType === 'select') {
            f.setting.isMulti = false;
          }

          let childComp = null;

          if (f.formType === 'tags') {
            childComp = h('biz-team-select', {
              props: {
                value: this.form[f.fieldName],
              },
              on: {
                input: (event) => this.update(event, 'tags'),
              },
            });
          } else if (f.formType === 'linkman') {
            childComp = h('linkman-search', {
              props: {
                field: f,
                value: this.form[f.fieldName],
                disableMap: true,
              },
              on: {
                update: (event) => {
                  this.update(event);
                },
                input: (event) => {
                  this.form[f.fieldName] = event.keyword;
                },
              },
            });
          } else if (f.formType === 'area') {
            childComp = h('base-dist', {
              props: {
                value: this.form[f.fieldName],
                showDefaultCountry: false
              },
              on: {
                input: (event) => this.update(event, 'dist'),
              },
            });
          } else if (f.formType === 'customer') {
            childComp = h('customer-search', {
              props: {
                field: f,
                value: this.form[f.fieldName],
                disableMap: true,
              },
              on: {
                update: (event) => this.update(event),
              },
            });
          } else if (f.formType === 'user') {
            childComp = h('user-search', {
              props: {
                field: f,
                value: this.form[f.fieldName],
                disableMap: true,
              },
              on: {
                update: (event) => this.update(event),
                input: (event) => {
                  this.form[f.fieldName] = event.keyword;
                },
              },
            });
          } else {
            childComp = h(
              comp.extend && comp.extend[`${f.formType}_search`]
                ? comp.extend[`${f.formType}_search`]
                : comp.build,
              {
                props: {
                  field: f,
                  value: this.form[f.fieldName],
                  disableMap: true,
                  placeholder: Utils.genPlaceholder(f),
                },
                on: {
                  update: (event) => this.update(event),
                },
              }
            );
          }

          return h(
            'form-item',
            {
              props: {
                label: f.displayName,
                needValidate: false,
              },
            },
            [childComp]
          );
        },
      },
      render(h) {
        return (
          <div class={`form-item-container ${this.columnNum == 2 ? 'two-columns' : ''}`}>
            {this.fields.map((f) => this.renderInput(h, f))}
          </div>
        );
      },
      components: { ...SettingComponents },
    },
  },
};
</script>
<style lang="scss">
.task-search-forms {
  transition: height 0.5s;
  .form-item {
    width: 340px !important;
  }
}
</style>
<style lang="scss" scoped>
.advanced-search-form {
  overflow: auto;
  padding: 10px 15px 150px 15px;

  height: calc(100%);
  justify-content: space-between;
  overflow-x: hidden;

  .two-columns {
    display: flex;
    flex-wrap: wrap;
    .el-form-item {
      width: 50%;
    }
  }

  .form-item-container {
    justify-content: space-between;
  }

  .form-item {
    label {
      padding-left: 0;
    }

    width: 390px;
  }

  .advanced-search-btn-group {
    display: flex;
    justify-content: flex-end;
    width: 100%;
    position: absolute;
    bottom: 0px;
    background: #fff;
    padding: 15px 20px;

    .base-button {
      margin: 0 10px;
    }
  }
}
.hide {
  overflow: hidden;
  padding: 0;
  height: 0;
  width: 0;
}
.task-search-panel-title {
  height: 54px;
  line-height: 54px;
  padding: 0 15px;
}
.task-search-guide {
  position: relative;
  left: 120px;
  margin-bottom: 20px;
  > div {
    &:first-child {
      width: 0;
      height: 0;
      border-left: 4px solid transparent;
      border-right: 4px solid transparent;
      border-bottom: 6px solid $color-primary-light-6;
      @include borderBottomColor();
      margin-left: 15px;
    }
    &:last-child {
      position: relative;
      width: 267px;
      height: 50px;
      font-size: 14px;
      color: #fff;
      line-height: 50px;
      @include backgroundColor();
      box-shadow: 0px 6px 16px 0px rgba(0, 0, 0, 0.08),
        0px 3px 6px -4px rgba(0, 0, 0, 0.12);
      text-align: center;
      border-radius: 4px;
      > span {
        font-size: 12px;
        font-family: fantasy;
        position: absolute;
        top: 8px;
        right: 9px;
        line-height: 10px;
      }
    }
  }
}

.cover {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 99;
  background-color: rgba(0, 0, 0, 0.6);
}
</style>

<style lang="scss">
.advanced-search-form {
  overflow: auto;
  padding: 10px 15px 63px 15px;

  height: calc(100% - 52px);
  justify-content: space-between;

  .two-columns {
    display: flex;
    flex-wrap: wrap;
    .el-form-item {
      width: 50%;
    }
  }

  .form-item-container {
    justify-content: space-between;
  }

  .form-item {
    label {
      padding-left: 0;
    }

    width: 390px;
  }

  .advanced-search-btn-group {
    display: flex;
    justify-content: flex-end;
    width: 100%;
    position: absolute;
    bottom: 0px;
    background: #fff;
    padding: 15px 20px;

    .base-button {
      margin: 0 10px;
    }
  }
}
</style>
